package com.plata.base.base;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.menu.myapplication.BuildConfig;

import java.util.List;

public class AbsBaseActivity extends AppCompatActivity {

    protected boolean isPaused = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setStatusBar();
        super.onCreate(savedInstanceState);
    }

    protected boolean isBlack() {
        return true;
    }

    protected void setStatusBar() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | (isBlack() ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_VISIBLE)
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void changeStatusBar(boolean isBlack) {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | (isBlack ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_VISIBLE)
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        isPaused = true;
    }

    @Override
    protected void onResume() {
        try {
            super.onResume();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        isPaused = false;
        print("resume " + this);
    }

    @Override
    protected void onStart() {
        try {
            super.onStart();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public ComponentName startService(Intent service) {
        try {
            return super.startService(service);
        } catch (Throwable e) {
            return null;
        }
    }

    public final boolean isPaused() {
        return isPaused;
    }

    public void replaceFragment(int id, Fragment fragment) {
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(id, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void hideFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void hideFragment(List<Fragment> fragment) {
        if (fragment == null || fragment.isEmpty()) {
            return;
        }
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            for (Fragment f : fragment) {
                transaction.hide(f);
            }
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void showFragment(Fragment fragment) {
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(fragment);
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void addFragment(int id, Fragment fragment) {
        if (fragment == null) {
            return;
        }
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                transaction.show(fragment);
            } else {
                transaction.add(id, fragment);
            }
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public void removeFragment(Fragment fragment) {
        removeFragment(getSupportFragmentManager(), fragment);
    }

    public void removeFragment(FragmentManager manager, Fragment fragment) {
        doRemoveFragment(manager, fragment);
    }

    private void doRemoveFragment(FragmentManager manager, Fragment f) {
        try {
            if (f instanceof Fragment == false) {
                return;
            }
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.remove(f);
            transaction.commitNowAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        try {
            return super.registerReceiver(receiver, filter);
        } catch (Throwable e) {
            e.printStackTrace();
            return new Intent();
        }
    }

    @Override
    public void unregisterReceiver(BroadcastReceiver receiver) {
        try {
            super.unregisterReceiver(receiver);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void unbindService(ServiceConnection conn) {
        try {
            super.unbindService(conn);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finish() {
        super.finish();
        print("finish " + this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        print("destroy " + this);
    }

    @Override
    public void startActivity(Intent intent) {
        try {
            super.startActivity(intent);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        try {
            super.startActivityForResult(intent, requestCode, options);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    void print(String msg) {
        if (BuildConfig.DEBUG) {
            Log.e("tagggg", msg);
        }
    }
}
